package com.mdg.bike;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.widget.ListView;
import android.widget.SimpleAdapter;


public class Directory extends Activity {
		
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.directory);
		
		ListView lv = (ListView) findViewById(R.id.dirlv);
		ArrayList<HashMap< String, String>> dirlist = new ArrayList<HashMap<String, String>>();
		String[] states = this.getResources().getStringArray(R.array.states);
		
		for(String s : states){
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("state_name", s);
			map.put("track_num", "Number of trails: " + 0);
			
			dirlist.add(map);
		}
		
		SimpleAdapter adapt = new SimpleAdapter(this, dirlist, android.R.layout.two_line_list_item, 
									new String[] {"state_name", "track_num"}, new int[] {android.R.id.text1, android.R.id.text2});
		
		lv.setAdapter(adapt);
		lv.setTextFilterEnabled(true);
		
		// Code to test using BLOBs in mysql
		getBlobData();
	}
	
	private void getBlobData(){
		simpleHTTPBlobStuff();
		jsonBlobStuff();
	}
	
	private void simpleHTTPBlobStuff(){
		/*
		HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://mainelyapps.com/mdg/testblob.php");
        List<String> ret = new ArrayList<String>();

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        UrlEncodedFormEntity formEntity;
		try {
			formEntity = new UrlEncodedFormEntity(params);
			request.setEntity(formEntity);
	        HttpResponse response;
			try {
				response = client.execute(request);
				 BufferedReader in = new BufferedReader(new InputStreamReader(response
	                        .getEntity().getContent()));

				 String line = "";
				 while ((line = in.readLine()) != null) {
					 String[] components = line.split(";");
					 byte[] data = components[2].getBytes("UTF8");
					 String[] hexBytes = new String[128];
					 for(int i = 0; i < data.length; i++){
						 Integer el = new Integer((int)data[i]);
						 hexBytes[i] = Integer.toHexString(el);
					 }
					 ret.add(line);
				 }

				 in.close();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
	}
	
	private void jsonBlobStuff(){
		HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost("http://mainelyapps.com/mdg/testjsonblob.php");
        List<String> ret = new ArrayList<String>();

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        UrlEncodedFormEntity formEntity;
		try {
			formEntity = new UrlEncodedFormEntity(params);
			request.setEntity(formEntity);
	        HttpResponse response;
			try {
				response = client.execute(request);
				 BufferedReader in = new BufferedReader(new InputStreamReader(response
	                        .getEntity().getContent()));

				 String line = "";
				 while ((line = in.readLine()) != null) {
					 try {
						JSONObject jsonO = new JSONObject(line);
						Integer id = jsonO.getInt("rowID");
						String name = jsonO.getString("pathName");
						String overlayStr = jsonO.getString("pathObject");
						byte[] decodedBytes = Base64.decode(overlayStr, Base64.DEFAULT);
						
						String[] hexBytes = new String[128];
						 for(int i = 0; i < decodedBytes.length; i++){
							 Integer el = new Integer((int)decodedBytes[i]);
							 hexBytes[i] = Integer.toHexString(el);
						 }
						System.out.println("Print this stuff");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 ret.add(line);
				 }

				 in.close();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
